// 初始化数据
// 初始化管理员
const { User, Role, MicroApp, MicroRouter } = require('../model');

const initAdmin = async () => {
  try {
    const [role, roleFlag] = await Role.findOrCreate({
      where: {
        id: 1,
      },
      defaults: {
        label: 'superAdmin',
        desc: '超级管理员',
        menu: '',
        id: 1,
      },
    });
    if (roleFlag) {
      console.log('初始化超级管理员角色');
    } else {
      console.log('超级管理员角色已存在');
    }

    const [modle, flag] = await User.findOrCreate({ where: { phone: '15139846521' }, defaults: { phone: '15139846521', pwd: '123456', role: 1, name: '超级管理员', nickname: '超级管理员', id: 1 } });
    if (flag) {
      console.log('初始化创建超级管理员');
    } else {
      console.log('超级管理员账号已存在,无需重复创建！');
    }
  } catch (error) {
    console.log('初始化管理员error', error);
  }
};

// 初始化微应用
const initMicro = async () => {
  try {
    const [micro, microFlag] = await MicroApp.findOrCreate({
      where: {
        id: 1,
      },
      defaults: {
        name: 'microAppName',
        code: 'microAppName',
        entry: 'http://127.0.0.1:8300/microAppName',
        activeRule: '/microAppName',
        childProps: { routerBase: '/microAppName' },
        id: 1,
      },
    });
    if (microFlag) {
      console.log('初始化微应用成功，success');
    } else {
      console.log('初始化微应用失败，此微应用已存在');
    }

    initRouter();
  } catch (error) {
    console.log('初始化微应用error', error);
  }
};

const routerList = [
  {
    id: 1,
    title: 'home',
    orderNum: 0,
    path: '/home',
    status: '1',
    type: '2',
    icon: 'AlignRightOutlined',
    parentId: '',
    meta: {
      title: 'home',
    },
    microAppId: '1',
  },
  {
    id: 2,
    title: '微应用1microAppRouter',
    orderNum: 0,
    path: '/microAppName',
    status: '1',
    type: '1',
    icon: 'AppleOutlined',
    parentId: '',
    meta: {
      title: '微应用1microAppRouter',
    },
    microAppId: '1',
  },
  {
    id: 3,
    title: '微应用管理',
    orderNum: 0,
    path: '/microAppName/microApp',
    status: '1',
    type: '2',
    icon: 'LaptopOutlined',
    parentId: '2',
    meta: {
      title: '微应用管理',
    },
    microAppId: '1',
  },
  {
    id: 4,
    title: '微应用路由管理',
    orderNum: 0,
    path: '/microAppName/microAppRouter',
    status: '1',
    type: '2',
    icon: 'GroupOutlined',
    parentId: '2',
    meta: {
      title: '微应用路由管理',
    },
    microAppId: '1',
  },
  {
    id: 5,
    title: 'icon',
    orderNum: 0,
    path: '/microAppName/icon',
    status: '1',
    type: '2',
    icon: 'AliwangwangFilled',
    parentId: '2',
    meta: {
      title: 'icon',
    },
    microAppId: '1',
  },
];

// 初始化路由
const initRouter = async () => {
  try {
    routerList.forEach(async element => {
      const [router, routerFlag] = await MicroRouter.findOrCreate({
        where: {
          path: element.path,
        },
        defaults: {
          ...element,
        },
      });
      if (routerFlag) {
        console.log('初始化路由成功');
      } else {
        console.log('初始化路由失败：已存在');
      }
    });
  } catch (error) {
    console.log('初始化路由error', error);
  }
};

module.exports = {
  initAdmin,
  initMicro,
};
